$(document).ready(function() {	
/*	findAllDeptType();
	findAllRole();*/
	findExpertArea();
	var eventId=$.trim($("#eventId").text());
	
	$(".disposalAdviceButton").click(function(){
		$("#deptChooseWindow").dialog('open');
	});
	
		// 指派部门窗体的定义
	$("#deptChooseWindow").dialog({
		autoOpen : false,
		position: 'top',
		minWidth : 700,
		modal : true,
		title : '申请处置建议',
//		buttons : {
//			'确定' : function() {
//				var url_applyAdvice = "/ag/work/AdviceApply_applyAdvice.action";
//				var str = "'";
//				$("#selDepts option").each(function() {
//							str += $(this).val() + "','";
//						});
//			
//				var applyString =$("#applyString").attr("value");
//			
//				var lastestTime=$("#lastestTime").attr("value");
//				var req_applyAdvice ={
//						'adviceExpertIds':str,
//						'eventIdString':eventId,
//						'lastestTime':lastestTime,
//						'applyString':applyString
//				};
//				if(checkLength(applyString, 0, 70)&&lastestTime!=null&&str!="'"){
//				$.post(url_applyAdvice,req_applyAdvice,function(data){
//					alert(data.result);
//				window.location.href="/ag/wzp/event_findById.action?event.EId="+eventId;
//				});
//				}else{
//					$(".applyStringLengthLimit").css('display','block');
//					$(".applyStringLengthLimit").css("background-color","#66CCFF");
//					
//				}
//			},
//			'取消' : function() {
//				$(this).dialog('close');
//				window.location.href="/ag/wzp/event_findById.action?event.EId="+eventId;
//			}
//		},
		close : function() {
			window.location.href="/ag/wzp/event_findById.action?event.EId="+eventId;
			return false;
		}
	});
	
	
	$("#selOk").click(function() {
		var url_applyAdvice = "/ag/work/AdviceApply_applyAdvice.action";
		var str = "'";
		$("#selDepts option").each(function() {
					str += $(this).val() + "','";
				});
	
		var applyString =$("#applyString").attr("value");
	
		var lastestTime=$("#lastestTime").attr("value");
		var req_applyAdvice ={
				'adviceExpertIds':str,
				'eventIdString':eventId,
				'lastestTime':lastestTime,
				'applyString':applyString
		};
		if(checkLength(applyString, 0, 70)&&lastestTime!=null&&str!="'"){
		$.post(url_applyAdvice,req_applyAdvice,function(data){
			alert(data.result);
		window.location.href="/ag/wzp/event_findById.action?event.EId="+eventId;
		});
		}else{
			$(".applyStringLengthLimit").css('display','block');
			$(".applyStringLengthLimit").css("background-color","#66CCFF");
		}
	});
	$("#selCancel").click(function(){
		$(this).dialog('close');
		window.location.href="/ag/wzp/event_findById.action?event.EId="+eventId;
	});
	
	
	function alert_result(str){
		$("#alert_result").empty();
		$("#alert_result").text(str).dialog('open');
	}
	
	$("#alert_result").dialog({
		autoOpen : false,
		minWidth : 300,
		modal : true,
		title : '返回',
		buttons : {
			'确定' : function() {
				window.location.href="/ag/wzp/event_findById.action?event.EId="+eventId;
			},
			'取消' : function() {
				
				window.location.href="/ag/wzp/event_findById.action?event.EId="+eventId;
			}
		},
		close : function() {
			window.location.href="/ag/wzp/event_findById.action?event.EId="+eventId;
			return false;
		}
		
	});
	
	$(".showSelectArea").click(function(){
		
		$("#selectArea").css('display','block');
	});


//	// 弹出任务部门指派的选择窗体
//	$('#chosedept').click(function() {
//		$("#deptChooseWindow").dialog("open");
//	});

	$("#dept").click(function(){
		$("#deptArea").show();
		$("#roleArea").hide();
		$("#subDepts").empty();
	});
	$("#role").click(function(){
		$("#deptArea").hide();
		$("#roleArea").show();
		$("#subDepts").empty();
	});
	// 指派部门的窗体上两个按钮的响应函数 addDept 和 delDept
	$("#addDept").click(function() {
		if ($("#subDepts option:selected").length > 0) {
			$("#subDepts option:selected").each(function() {
				var strHtml="";
				var id="",name=""
				var udId = $(this).val();
				var udName = $(this).text();
				/*
				if($("#selDepts option").length == 1){
					alert("只能选择一个人！");
					return ;
				}
				*/
				if ($("#selDepts option").length > 0 ) {
					var t = true;
					$("#selDepts option").each(function(){
						if(udId == $(this).val()){ 
							t = false;
						}
					})
					if(t){
						id=udId;
						name = udName
						strHtml+="<option class='class1' name='selDepts' selected value='" + udId + "'>"
							+ udName + "</option>";
					}
				}else{
					id=udId;
					name = udName
					strHtml+="<option class='class1' name='selDepts' selected value='" + udId + "'>"
						+ udName + "</option>";
				}
				$("#selDepts").append(strHtml);
			})
		}
	});

	$("#delDept").click(function() {
		if ($("#selDepts option:selected").length > 0) {
			$("#selDepts option:selected").each(function() {
				$(this).remove();
			})
		}
	});
	/**
	 * 专家领域下拉列表
	 */
	$("#expertAreaList").change(function(){
		var area_select = $("#expertAreaList option:selected").attr("value");
		$.ajax({
			type:"post",
			url:"/ag/work/AdviceApply_findExpertByArea.action",
			data:{'expertAreaIdString':area_select},
			success:function(data){
				$("#subDepts").empty();
				$.each(data.expertList,function(i,n){
					$("#subDepts").append("<option value="+n.expId+">"+n.expName+"</option>");
				});
			}
		});
	});
});

	
	//获取所有专家领域
	function findExpertArea(){
		$.ajax({
			type:"post",
			url:"/ag/work/AdviceApply_findExpertArea.action",
			success:function(data){
				$.each(data.expertAreaList,function(i,n){
					$("<option value="+n.expareaId+">"+n.expareaName+"</option>").appendTo("#expertAreaList");
				});
			}
		});

	}
	
	//判断字符串o的长度是不是在min和max之间,汉字当两个字符
	function checkLength(o, min, max) {
		var dtDealingType1 = $.trim(o);
		var cArr1 = dtDealingType1.match(/[^x00-xff]/ig);
		var the_length = dtDealingType1.length
				+ (cArr1 == null ? 0 : cArr1.length);
		if (the_length > max || the_length < min) {
			return false;
		} else {
			return true;
		}
	}